﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Drawer.cs" company="HelloWorld">
//   BY苏宣
// </copyright>
// <summary>
//   Defines the Drawer type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Control
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    /// <summary>
    /// The drawer.
    /// </summary>
    public partial class Drawer : UserControl
    {
        /// <summary>
        /// The open index.
        /// </summary>
        private int openIndex;

        /// <summary>
        /// Initializes a new instance of the <see cref="Drawer"/> class.
        /// </summary>
        public Drawer()
        {
            this.InitializeComponent();
            this.Nodes1 = new List<TreeNode>();
            this.Nodes2 = new List<TreeNode>();
            this.Nodes3 = new List<TreeNode>();
            this.SelectedText = string.Empty;
        }

        /// <summary>
        /// Gets or sets the nodes 1.
        /// </summary>
        public List<TreeNode> Nodes1 { get; set; }

        /// <summary>
        /// Gets or sets the nodes 2.
        /// </summary>
        public List<TreeNode> Nodes2 { get; set; }

        /// <summary>
        /// Gets or sets the nodes 3.
        /// </summary>
        public List<TreeNode> Nodes3 { get; set; }

        /// <summary>
        /// Gets or sets the selected text.
        /// </summary>
        public string SelectedText { get; set; }

        // 当外部更改时，抽屉也更改。

        /// <summary>
        /// The change.
        /// </summary>
        public void Change()
        {
            if (this.openIndex == 1)
            {
                this.SwitchIndex(this.Nodes1);
            }

            if (this.openIndex == 2)
            {
                this.SwitchIndex(this.Nodes2);
            }

            if (this.openIndex == 3)
            {
                this.SwitchIndex(this.Nodes3);
            }
        }

        // 转换当前打开的抽屉到文字，以便确定路径。

        /// <summary>
        /// The get part.
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetPart()
        {
            switch (this.openIndex)
            {
                case 1:
                    return "公共";
                case 2:
                    return "笔记";
                case 3:
                    return "资源";
                default:
                    return string.Empty;
            }
        }

        // 获取当前点击的节点

        /// <summary>
        /// The tree view 1_ after select.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.SelectedText = e.Node.Text;
        }

        // 切换目录

        /// <summary>
        /// The switch index.
        /// </summary>
        /// <param name="nodes">
        /// The nodes.
        /// </param>
        private void SwitchIndex(IEnumerable<TreeNode> nodes)
        {
            this.treeView1.Nodes.Clear();
            foreach (var node in nodes)
            {
                this.treeView1.Nodes.Add(node);
            }
        }

                /// <summary>
        /// The button 1_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button1_Click(object sender, EventArgs e)
        {
            if (this.openIndex == 1)
            {
                this.button1.Dock = DockStyle.Top;
                this.button2.Dock = DockStyle.Top;
                this.button3.BringToFront();
                this.button3.Dock = DockStyle.Top;

                this.openIndex = 0;
                this.treeView1.Visible = false;
            }
            else
            {
                this.button1.Dock = DockStyle.Top;
                this.button2.Dock = DockStyle.Bottom;
                this.button3.SendToBack();
                this.button3.Dock = DockStyle.Bottom;

                this.treeView1.BringToFront();
                this.treeView1.Dock = DockStyle.Bottom;
                this.SwitchIndex(this.Nodes1);
                this.openIndex = 1;
                this.treeView1.Visible = true;
            }
        }

        /// <summary>
        /// The button 2_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button2_Click(object sender, EventArgs e)
        {
            if (this.openIndex == 2)
            {
                this.button1.Dock = DockStyle.Top;
                this.button2.Dock = DockStyle.Top;
                this.button3.BringToFront();
                this.button3.Dock = DockStyle.Top;

                this.openIndex = 0;
                this.treeView1.Visible = false;
            }
            else
            {
                this.button2.Dock = DockStyle.Top;
                this.button1.SendToBack();
                this.button1.Dock = DockStyle.Top;
                this.button3.Dock = DockStyle.Bottom;

                this.treeView1.BringToFront();
                this.treeView1.Dock = DockStyle.Bottom;
                this.SwitchIndex(this.Nodes2);

                this.openIndex = 2;
                this.treeView1.Visible = true;
            }
        }

        /// <summary>
        /// The button 3_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button3_Click(object sender, EventArgs e)
        {
            if (this.openIndex == 3)
            {
                this.button1.Dock = DockStyle.Top;
                this.button2.Dock = DockStyle.Top;
                this.button3.BringToFront();
                this.button3.Dock = DockStyle.Top;
                this.openIndex = 0;
                this.treeView1.Visible = false;
            }
            else
            {
                this.button3.SendToBack();
                this.button3.Dock = DockStyle.Top;
                this.button2.SendToBack();
                this.button2.Dock = DockStyle.Top;
                this.button1.SendToBack();
                this.button1.Dock = DockStyle.Top;
                this.treeView1.BringToFront();
                this.treeView1.Dock = DockStyle.Bottom;
                this.SwitchIndex(this.Nodes3);

                this.openIndex = 3;
                this.treeView1.Visible = true;
            }
        }
    }
}
